// src/stores/configStore.js
import { defineStore } from 'pinia'
import { ConfigApi } from '@/api/config'
import { ElMessage } from 'element-plus'

export const useConfigStore = defineStore('config', {
    state: () => ({
        configList: [],
        publicConfig: {},
        historyList: [],
        pageInfo: {
            page: 1,
            size: 10,
            total: 0
        }
    }),

    actions: {
        // 获取所有配置
        async getAllConfig() {
            try {
                const response = await ConfigApi.getAllConfig()
                if (response && response.code === 200) {
                    this.configList = response.data
                    return response
                } else {
                    throw new Error(response?.message || '获取配置失败')
                }
            } catch (error) {
                ElMessage.error('获取配置失败: ' + (error.message || '网络错误'))
                throw error
            }
        },

        // 分页获取配置列表
        async getConfigListWithPagination(params) {
            try {
                const response = await ConfigApi.getConfigListWithPagination(params)
                if (response && response.code === 200) {
                    this.configList = response.data.records || []
                    this.pageInfo.page = response.data.pageIndex || response.data.pageNumber || params.pageIndex || 1
                    this.pageInfo.size = response.data.pageSize || params.pageSize || 10
                    this.pageInfo.total = response.data.total || response.data.totalRow || 0
                    return response
                } else {
                    throw new Error(response?.message || '获取配置失败')
                }
            } catch (error) {
                ElMessage.error('获取配置失败: ' + (error.message || '网络错误'))
                throw error
            }
        },

        // 更新配置项
        async updateConfig(config) {
            try {
                const data = {
                    id: config.id,
                    configKey: config.configKey || config.config_key,
                    configValue: config.configValue || config.config_value,
                    configDesc: config.configDesc || config.config_desc,
                    createdTime: config.createdTime || config.created_time,
                    updatedTime: config.updatedTime || config.updated_time,
                    en: config.en
                }

                const response = await ConfigApi.updateConfig(data)
                if (response && response.code === 200) {
                    ElMessage.success('更新成功')
                    return response
                } else {
                    throw new Error(response?.message || '更新失败')
                }
            } catch (error) {
                ElMessage.error('更新失败: ' + (error.message || '网络错误'))
                throw error
            }
        },

        // 刷新缓存
        async refreshCache() {
            try {
                const response = await ConfigApi.refreshCache()
                if (response && response.code === 200) {
                    ElMessage.success('刷新缓存成功')
                    return response
                } else {
                    throw new Error(response?.message || '刷新缓存失败')
                }
            } catch (error) {
                ElMessage.error('刷新缓存失败: ' + (error.message || '网络错误'))
                throw error
            }
        },

        // 获取公共配置
        async getPublicConfig() {
            try {
                const response = await ConfigApi.getPublicConfig()
                if (response && response.code === 200) {
                    this.publicConfig = response.data
                    return response
                } else {
                    throw new Error(response?.message || '获取公共配置失败')
                }
            } catch (error) {
                ElMessage.error('获取公共配置失败: ' + (error.message || '网络错误'))
                throw error
            }
        },

        // 获取配置历史记录
        async getConfigHistory(params) {
            try {
                const response = await ConfigApi.getConfigHistory(params)
                if (response && response.code === 200) {
                    this.historyList = response.data.records || []
                    this.pageInfo.total = response.data.total || response.data.totalRow || 0
                    return response
                } else {
                    throw new Error(response?.message || '获取历史记录失败')
                }
            } catch (error) {
                ElMessage.error('获取历史记录失败: ' + (error.message || '网络错误'))
                throw error
            }
        }
    }
})
